<?php

class Ext_Validate
{
    protected $_validators = array();
    protected $_messages   = array();

    public function __construct()
    {
    }
    
    public function addValidater(Ext_Validate_Interface $validator, $breakChainOnFailure = false)
    {
        $this->_validators[] = array(
            'instance'            => $validator,
            'breakChainOnFailure' => (boolean) $breakChainOnFailure
            );
        
        return $this;
    }
    
    public function isValid()
    {
        $result = true;
        foreach ($this->_validators as $element) {
            $validator = $element['instance'];
            if ($validator->isValid()) {
                continue;
            }
            $result = false;
            $messages = $validator->getMessages();
            $this->addMessages($messages);
            
            if ($element['breakChainOnFailure']) {
                break;
            }
        }
        return $result;
    }
    
    public function addMessage($message)
    {
        $this->_messages[] = $message;
    }
    
    public function addMessages($messages)
    {
        $this->_messages = array_merge($this->_messages, $messages);
    }
    
    public function getMessages()
    {
        return $this->_messages;
    }
}
